package com.sky.service.impl;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.enumeration.context.BaseContext;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;




    /**
     *
     * 添加购物车
     * @param shoppingCartDTO
     * */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        /*
        * 1.查询购物车判断当前添加商品在购物车中是否存在
        * 2.判断添加的是菜品还是套餐
        * 3.向表中插入数据
        * */
        ShoppingCart shoppingCart = new ShoppingCart();
        //此时新创建的ShoppingCart对象属性是空的，我们要进行初始化
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //添加登录用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //判断购物车
        if(list != null && list.size() > 0) {
            //表示购物车中有数据：
            //为什么是一条呢？因为用户添加到购物车的时候不可能一下添加多个菜品，
            // 是一个一个添加的，至于每次添加的是菜品或是套餐都行，插入进ShoppingCart表中只会是一种之一
            //所以只用判断购物车里面有没有数据就行，不用判断有段少条
            //取出要添加的对象，(这个对象只可能是菜品或者套餐)
            //然后对其执行amount++操作,也就是对其进行修改操作
            ShoppingCart product = list.get(0);
            product.setNumber(product.getNumber() + 1);
            shoppingCartMapper.update(product);
        }else{
            //表示购物车没有数据，这时我们就可以将用户选择的菜品或套餐添加进购物车
            //判断添加到购物车的是菜品还是套餐(执行不同的添加操作)
            Long dishId = shoppingCart.getDishId();
            if(dishId != null && dishId > 0) {
                //添加菜品相关数据和购物车的其他属性
                //根据dishId查询对应菜品的name、image、Flavors、price
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                //设置口味

            }else{
                Long setmealId = shoppingCart.getSetmealId();
                //添加套餐相关数据和购物车的其他属性
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将封装好的数据添加进表中
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> show() {
        Long userId =BaseContext.getCurrentId();//获取当前微信用户Id
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanAll(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //1.根据前端传来数据动态查询目标商品(菜品or套餐)
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        //2.获取对应的商品数量
        Integer number = cart.getNumber();
        //3.判断商品数量
        if(number > 1) {
            // >1:number-1
            cart.setNumber(number - 1);
            shoppingCartMapper.update(cart);
        }else {
            // =1:delete(删除全部数据也就是ShoppingCart对象)
            //商品对象cart的dishId和setmealId必有一个为null,动态删除
            shoppingCartMapper.delete(cart);
        }
        //逻辑分析


    }
}
